package cn.ococci.dm.Main.Adapter.Base;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cyan on 15/9/23.
 *  一个实现BaseAdapter的抽象类
 */
public abstract class AbBaseAdapter<T, H extends BaseViewHolder> extends BaseAdapter {

    protected Context mContext;
    protected int layoutResId;

    protected List<T> mData;

    protected IMultiItemType<T> mMultiItemSupport;

    public AbBaseAdapter(Context c,int layoutResId){
        this(c,layoutResId,null);
    }

    public AbBaseAdapter(Context c,int layoutResId,List<T> data){
        this.mContext = c;
        this.layoutResId = layoutResId;
        this.mData = data == null?new ArrayList<T>():new ArrayList<>(data);
    }

    public AbBaseAdapter(Context context, ArrayList<T> data,IMultiItemType<T> multiItemSupport){
        this.mContext = context;
        this.mData = data == null ? new ArrayList<T>() : new ArrayList<>(data);
        this.mMultiItemSupport = multiItemSupport;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public T getItem(int position) {
        if (position >= mData.size())
            return null;
        return mData.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if(mMultiItemSupport != null){  // 返回类型的个数
            return mMultiItemSupport.getItemViewType(position,mData.get(position));
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        if (mMultiItemSupport != null){
            return mMultiItemSupport.getViewTypeCount();
        }
        return super.getViewTypeCount();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final H h = getAdapterHelper(position, convertView, parent);
        T t = getItem(position);
        h.setRelationsObject(t);
        convert(h, t,position);

        return h.getConvertView();
    }

    protected abstract H getAdapterHelper(int position, View convertView, ViewGroup parent);

    protected abstract void convert(H h,T t,int pos);
}
